package com.vanke.kotlin_mvp

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * @name Kotlin_MVP
 * @package name：com.vanke.kotlin_mvp
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/8/17 10:14
 */
object DelegateExt {
    fun <T> notNullSingleValueVar(): ReadWriteProperty<Any?, T> = NotNullSingleValueVar()
}

class NotNullSingleValueVar<T> : ReadWriteProperty<Any?, T> {

    private var value: T? = null
    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value ?: throw IllegalStateException()
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        this.value = if (this.value == null) value
        else throw IllegalStateException()
    }
}
